using System;
using System.Xml.Serialization;
using NUnit.Framework;

namespace ValidationFramework.Tests
{
    [TestFixture]
    public class RequiredObjectRuleAttributeTest
    {

        [XmlRoot("person")]
        public class Person : IEquatable<Person>
        {
            [XmlAttribute("name")]
            public string Name
            {
                get;
                set;
            }

            [XmlAttribute("age")]
            public int Age
            {
                get;
                set;
            }

            public bool Equals(Person other)
            {
                return Name == other.Name && Age == other.Age;
            }
        }

        [Test]
        public void CheckDefaultValues()
        {
            var ruleAttribute = new RequiredObjectRuleAttribute();

            var propertyRule = (RequiredRule<Person>)ruleAttribute.CreateInstance(new MockPropertyDescriptor<Person>("foo"));
            var parameterRule = (RequiredRule<Person>)ruleAttribute.CreateInstance(new MockParameterDescriptor<Person>("foo"));
			var fieldRule = (RequiredRule<Person>)ruleAttribute.CreateInstance(new MockFieldDescriptor<Person>("foo"));

        }

        [Test]
        public void CheckNonDefaultValues()
        {
            var expectedPerson = new Person {Name = "john", Age = 23};
            const string validatorXml = "<person name='john' age='23'/>";
            var ruleAttribute = new RequiredObjectRuleAttribute();
            const string expectedInitialValue = validatorXml;
            ruleAttribute.InitialValue = expectedInitialValue;
            Assert.AreEqual(expectedInitialValue, ruleAttribute.InitialValue);

            var propertyRule = (RequiredRule<Person>)ruleAttribute.CreateInstance(new MockPropertyDescriptor<Person>("foo"));
            var parameterRule = (RequiredRule<Person>)ruleAttribute.CreateInstance(new MockParameterDescriptor<Person>("foo"));
			var fieldRule = (RequiredRule<Person>)ruleAttribute.CreateInstance(new MockFieldDescriptor<Person>("foo"));

            Assert.IsTrue(expectedPerson.Equals(propertyRule.InitialValue));

            Assert.IsTrue(expectedPerson.Equals(parameterRule.InitialValue));

			Assert.IsTrue(expectedPerson.Equals(fieldRule.InitialValue));
        }


        [Test]
        public void CallAttributeTester()
        {
            AttributeTester.CheckDefaultValues<object>(new RequiredObjectRuleAttribute(), true, true, true);
            AttributeTester.CheckNonDefaultValues<object>(new RequiredObjectRuleAttribute(), true, true, true);
        }
    }
}